﻿using PokemonClientToos.ActionFile;
using PokemonClientToos.Entity;
using PokemonClientToos.Frm;
using PokemonClientToos.TempModel;
using PokemonTools.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PokemonClientToos
{
    public partial class frm_main : Form
    {
        public frm_main()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_login_Click(object sender, EventArgs e)
        {
            btn_login.Enabled = false;
            if (btn_login.Text.Equals("登录"))
            {
                string acount = txt_acount.Text;
                string password = txt_password.Text;
                if (string.IsNullOrWhiteSpace(acount) || string.IsNullOrWhiteSpace(password))
                {
                    MessageBox.Show("请输入帐号与密码");
                    txt_acount.Focus();
                    txt_acount.SelectAll();
                    return;
                }
                if (ActionCall.instance.Action400(acount, password))
                {
                    Robot.instance.SocketConnect();
                    if (Robot.clientSocket != null)
                    {
                        ActionCall.instance.Action1000(delegate()
                        {
                            lbl_sid.Text = GameRole.loginInfo.Sid.ToString();
                            lbl_rid.Text = GameRole.loginInfo.Rid.ToString();
                        });

                    }
                }
                btn_login.Text = "注销";
            }
            else
            {
                Robot.instance.StopConnect();
                btn_login.Text = "登录";
            }
            btn_login.Enabled = true;
        }

        private void frm_main_Load(object sender, EventArgs e)
        {
            AllCache.instance.Load();
            LoadTreeViewNode();
        }

        private void tsm_actionSetting_Click(object sender, EventArgs e)
        {
            frm_actionSetting frm = new frm_actionSetting();
            frm.StartPosition = FormStartPosition.CenterParent;
            frm.ShowDialog();
            if (frm.DialogResult == System.Windows.Forms.DialogResult.Cancel)
            {
                LoadTreeViewNode();
            }
        }

        private void LoadTreeViewNode()
        {
            if (AllCache.instance.actionList != null)
            {
                tv_action.Nodes.Clear();
                TreeNode treeNode = new TreeNode();
                treeNode.Text = "接口";
                tv_action.Nodes.Add(treeNode);

                foreach (var item in AllCache.instance.actionList)
                {
                    TreeNode tn = new TreeNode(item.ActionID.ToString());
                    tn.ToolTipText = string.Format("接口编号:{0};参数:{1};是否有循环参数:{2};循环参数数量:{3};循环参数类型:{4};", item.ActionID, item.Parameters, item.IsLoopPar ? "有" : "无", item.LoopParNum, item.LoopParameters);
                    treeNode.Nodes.Add(tn);
                }
                treeNode.Expand();
            }
        }

        private void tv_action_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(e.Node.ToolTipText.Trim()))
            {
                dgv_parameters.Rows.Clear();
                int actionId = Convert.ToInt32(e.Node.Text.Trim());
                TempAction tempAction = AllCache.instance.actionList.Find(o => o.ActionID == actionId);
                // 生成参数列表
                if (!string.IsNullOrWhiteSpace(tempAction.Parameters))
                {
                    string[] arrPar = tempAction.Parameters.Split(',');
                    if (arrPar.Length > 0)
                    {
                        foreach (var item in arrPar)
                        {
                            DataGridViewRow row1 = new DataGridViewRow();
                            int rowIndex = dgv_parameters.Rows.Add(row1);
                            dgv_parameters.Rows[rowIndex].Cells[0].Value = item;
                        }
                    }
                    if (tempAction.IsLoopPar)
                    {
                        DataGridViewRow row2 = new DataGridViewRow();
                        row2.DefaultCellStyle.BackColor = Color.Red;
                        int rowIndex2 = dgv_parameters.Rows.Add(row2);
                        dgv_parameters.Rows[rowIndex2].Cells[0].Value = "int";
                        dgv_parameters.Rows[rowIndex2].Cells[1].Value = tempAction.LoopParNum;

                        string[] arrLoopPar = tempAction.LoopParameters.Split(',');
                        if (arrLoopPar.Length > 0)
                        {
                            for (int i = 0; i < tempAction.LoopParNum; i++)
                            {
                                foreach (var item in arrLoopPar)
                                {
                                    DataGridViewRow row3 = new DataGridViewRow();
                                    if (AllCache.colorList.Count > i)
                                    {
                                        row3.DefaultCellStyle.BackColor = AllCache.colorList[i];
                                    }
                                    int rowIndex3 = dgv_parameters.Rows.Add(row3);
                                    dgv_parameters.Rows[rowIndex3].Cells[0].Value = item;
                                }
                            }
                        }
                    }
                }
            }
        }

        private void btn_actionRequest_Click(object sender, EventArgs e)
        {
            Bytebuffer buffer = new Bytebuffer(false);
            buffer.ActionId = Convert.ToInt16(tv_action.SelectedNode.Text.Trim());
            buffer.WriteInt(GameRole.loginInfo.Sid);
            List<Object> parList = new List<object>();
            foreach (DataGridViewRow item in dgv_parameters.Rows)
            {
                switch (item.Cells[0].Value.ToString())
                {
                    case "int": buffer.WriteInt(Convert.ToInt32(item.Cells[1].Value)); break;
                    case "string": buffer.WriteString(Convert.ToString(item.Cells[1].Value)); break;
                    case "byte": buffer.WriteByte(Convert.ToByte(item.Cells[1].Value)); break;
                    case "int64": buffer.WriteInt(Convert.ToInt32(item.Cells[1].Value)); break;
                }
            }
            ActionCall.instance.ActionTcp(buffer, null);
        }

        private void btn_userPet_Click(object sender, EventArgs e)
        {

        }
    }
}